package com.dyna.web.lock.controller;

import com.dyna.domain.resp.R;
import com.dyna.domain.resp.ResultCode;
import com.dyna.web.lock.service.LockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhoucaiwang
 * @date 2024/5/21
 */

@Api(value = "demo模块/web/锁")
@RestController
@RequestMapping("/lock")
public class LockController {

    private final LockService lockService;

    @Autowired
    public LockController(LockService lockService) {
        this.lockService = lockService;
    }

    @ApiOperation(value = "读锁")
    @GetMapping("/read")
    public R<Object> readLock() {
        return R.data(lockService.readLock());
    }

    @ApiOperation(value = "写锁")
    @GetMapping("/write")
    public R<?> writeLock(@RequestParam(value = "text") String text) {
        lockService.writeLock(text);
        return R.success(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "锁竞争")
    @GetMapping("/contention")
    public R<Boolean> contention() {
        return R.isSuccess(lockService.contention());
    }
    
}

